home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / amiga / v3_1 / sbp3_1e.lzh / WHPARSER.PL < prev    next >
Text File  |  1991-10-31  |  3KB  |  83 lines

  1. /* From the book PROLOG PROGRAMMING IN DEPTH
  2.    by Michael A. Covington, Donald Nute, and Andre Vellino.
  3.    Copyright 1988 Scott, Foresman & Co.
  4.    Non-commercial distribution of this file is permitted. */
  5. /* Modified for Quintus Prolog by Andreas Siebert */
  6.  
  7. /* WHPARSER.PL */
  8.  
  9. /* Parser that handles WH-questions as well as statements.
  10.    For simplicity, we neglect morphology. */
  11.  
  12. /* Each phrase that can contain a WH-word has 3 arguments:
  13.      (1) List of WH-words found before starting to
  14.           parse this constituent;
  15.      (2) List of WH-words still available after
  16.           parsing this constituent;
  17.      (3) Structure built while parsing this
  18.           constituent (as in STRUCTUR.PL). */
  19.  
  20. /* Queries will use built-in predicate phrase/2 */
  21.  
  22. sentence(X,Z,sentence(NP,VP)) -->  /* Sentence that does not */
  23.      noun_phrase(X,Y,NP),          /* begin with a WH-word,  */
  24.      verb_phrase(Y,Z,VP).          /* but may be embedded in */
  25.                                    /* a sentence that does   */
  26.  
  27. sentence(X,Z,sentence(NP,VP)) -->
  28.      wh_word(W),                /* Sentence begins with WH-word. */
  29.      [did],                     /* Put the WH-word on the list,  */
  30.      noun_phrase([W|X],Y,NP),   /* absorb "did," and continue.   */
  31.      verb_phrase(Y,Z,VP).
  32.  
  33.  
  34. noun_phrase(X,X,noun_phrase(D,N)) -->
  35.      determiner(D),                /* Ordinary NP that does */
  36.      noun(N).                      /* not use saved WH-word */
  37.  
  38. noun_phrase([X|Tail],Tail,noun_phrase(X)) --> [].
  39.      /* Missing NP supplied by picking a */
  40.      /* stored WH-word off the list      */
  41.  
  42.  
  43. verb_phrase(X,Z,verb_phrase(V,NP)) -->
  44.      verb(V),
  45.      noun_phrase(X,Z,NP).
  46.  
  47. verb_phrase(X,Z,verb_phrase(V,S)) -->
  48.      verb(V),
  49.      sentence(X,Z,S).
  50.  
  51. determiner(determiner(a))   --> [a].
  52. determiner(determiner(the)) --> [the].
  53.  
  54. noun(noun(dog))  --> [dog].
  55. noun(noun(cat))  --> [cat].
  56. noun(noun(boy))  --> [boy].
  57. noun(noun(girl)) --> [girl].
  58.  
  59. /* Two forms of every verb:
  60.    "The boy saw the cat" vs. "Did the boy see the cat?" */
  61.  
  62. verb(verb(chased))   --> [chased];[chase].
  63. verb(verb(saw))      --> [saw];[see].
  64. verb(verb(said))     --> [said];[say].
  65. verb(verb(believed)) --> [believed];[believe].
  66.  
  67. wh_word(who)  --> [who].
  68. wh_word(what) --> [what].
  69.  
  70.  
  71. /* Sample queries */
  72.  
  73. demo1 :- phrase(sentence([],[],Structure),
  74.            [who,did,the,boy,believe,the,girl,saw]),
  75.          write(Structure),
  76.          nl.
  77.  
  78. demo2 :- phrase(sentence([],[],Structure),
  79.            [who,did,the,boy,believe,saw,the,girl]),
  80.          write(Structure),
  81.          nl.
  82. 
  83.